package com.xteam.youin_supplier.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.adpter.TenderListViewAdapter;
import com.xteam.youin_supplier.adpter.TenderResultListViewAdapter;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BeddingListItem;
import com.xteam.youin_supplier.model.BeddingResulListItem;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;


/**
 * 竞标结果
 */
@EFragment(R.layout.fragment_tender_result)
public class ResultFragment extends BaseFragment {


    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.listview)
    RecyclerView listview;

    TenderResultListViewAdapter tenderResultListViewAdapter;
    ResultFragment resultFragment;

    @AfterViews
    @Override
    void initView() {
        resultFragment = this;
        backBtn.setVisibility(View.GONE);
        topTitle.setText(getResources().getString(R.string.bidding_result_title));

        listview.setLayoutManager(new LinearLayoutManager(getActivity()));
        loadData();
    }

    //加载数据
    private void loadData() {
        requestParams = "supplier_id=" + GlobalData.userInfo.getData().getSupplier_id();
        requestUrl = RequestAddr.biddingResutlList + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    //处理数据
    BeddingResulListItem beddingResulListItem;
    private void dealData(Object object) {
        try {
            beddingResulListItem = gson.fromJson(object.toString(), BeddingResulListItem.class);
        } catch (JsonSyntaxException e) {
        Toast.makeText(getActivity(), R.string.info_empty, Toast.LENGTH_SHORT).show();
        return;
    }
        tenderResultListViewAdapter = new TenderResultListViewAdapter(getActivity().getApplicationContext(), beddingResulListItem);
        listview.setAdapter(tenderResultListViewAdapter);
        tenderResultListViewAdapter.setOnItemClickListener(new TenderResultListViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MainActivity.getInstance().switchFragment(resultFragment,
                        TenderResultFragment_.builder()
                                .competitive_id(beddingResulListItem.getData().get(position).getCompetitive_id())
                                .build());
            }
        });
    }
    @Override
    void Click(View view) {

    }
}
